package com.yaolaizaixian.merchant.home;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.victor.loading.rotate.RotateLoading;
import com.yaolaizaixian.merchant.R;
import com.yaolaizaixian.merchant.adapter.RightsAdapter;
import com.yaolaizaixian.merchant.base.BaseActivity;
import com.yaolaizaixian.merchant.base.BaseInfo;
import com.yaolaizaixian.merchant.base.BaseModel;
import com.yaolaizaixian.merchant.bean.RightsDetail;
import com.yaolaizaixian.merchant.bean.RightsDetailInfo;
import com.yaolaizaixian.merchant.utils.GsonUtil;
import com.yaolaizaixian.merchant.utils.LogUtil;
import com.yaolaizaixian.merchant.utils.MyNetUtils;
import com.yaolaizaixian.merchant.utils.SharedPreferencesUtil;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class RightsActivity extends BaseActivity {


    @BindView(R.id.ll_back)
    AutoLinearLayout llBack;
    @BindView(R.id.tv_title_text)
    TextView tvTitleText;
    @BindView(R.id.tv_rights_all)
    TextView tvRightsAll;
    @BindView(R.id.iv_rightslist1)
    ImageView ivRightslist1;
    @BindView(R.id.rl_rights_all)
    AutoRelativeLayout rlRightsAll;
    @BindView(R.id.tv_rights_bulu)
    TextView tvRightsBulu;
    @BindView(R.id.iv_orderlist2)
    ImageView ivOrderlist2;
    @BindView(R.id.rl_rights_bulu)
    AutoRelativeLayout rlRightsBulu;
    @BindView(R.id.ll_rights_twotitle)
    AutoLinearLayout llRightsTwotitle;
    @BindView(R.id.xrv_rightslist)
    XRecyclerView xrvRightslist;
    @BindView(R.id.iv_kong)
    ImageView ivKong;
    @BindView(R.id.rl_kong)
    AutoRelativeLayout rlKong;
    @BindView(R.id.ll_rights_total)
    AutoLinearLayout llRightsTotal;
    @BindView(R.id.ll_rights_all)
    AutoLinearLayout llRightsAll;
    @BindView(R.id.ll_rights_stayok)
    AutoLinearLayout llRightsStayok;
    @BindView(R.id.iv_rightslist)
    ImageView ivRightslist;
    @BindView(R.id.ll_lei)
    AutoLinearLayout llLei;
    @BindView(R.id.ll_member_total)
    AutoLinearLayout llMemberTotal;
    @BindView(R.id.ll_member_all)
    AutoLinearLayout llMemberAll;
    @BindView(R.id.ll_member_stayok)
    AutoLinearLayout llMemberStayok;
    @BindView(R.id.ll_member_staypay)
    AutoLinearLayout llMemberStaypay;
    @BindView(R.id.ll_member_makeup)
    AutoLinearLayout llMemberMakeup;
    @BindView(R.id.ll_member_bgold)
    AutoLinearLayout llMemberBgold;
    @BindView(R.id.iv_memberlist)
    ImageView ivMemberlist;
    @BindView(R.id.ll_member)
    AutoLinearLayout llMember;
    @BindView(R.id.rotateloading)
    RotateLoading rotateloading;
    private String TAG = "RightsActivity";
    private RightsAdapter rightsAdapter;

    //标记类型
    private boolean classflag = true;
    //标记等级
    private boolean memberflag = true;
    private String shopsysno;
    //1 抵扣 2 抵用
    private String type = "";
    //100注册会员 200 白金尊荣会员 300 尊荣会员 400 钻石会员 500 黑金会员
    private String customerLevel = "";
    private List<RightsDetailInfo> erList;
//    private ArrayList<RightsDetailInfo> totalorderLists;

    private Handler mhandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {

                case 1:

                    String result = (String) msg.obj;
                    BaseModel baseModel = GsonUtil.jsonToBean(result, BaseModel.class);
                    LogUtil.e(TAG, result);

                    if (baseModel.isFlag()) {
                        RightsDetail rightsDetail = GsonUtil.jsonToBean(result, RightsDetail.class);

                        erList = rightsDetail.getData().getErList();

                        if (erList.size()<=0){
                            rlKong.setVisibility(View.VISIBLE);
                        }else {
                            rlKong.setVisibility(View.GONE);
                        }


                        rightsAdapter = new RightsAdapter(RightsActivity.this, erList);
                        xrvRightslist.setAdapter(rightsAdapter);
                        rotateloading.stop();



                        rightsAdapter.setOnItemClickListener(new RightsAdapter.OnItemClickListener() {
                            @Override
                            public void onClick(int position) {

                                Intent intent = new Intent(RightsActivity.this, RightsDetailActivity.class);
                                intent.putExtra("id", erList.get(position).getSysno());
                                startActivity(intent);
                            }

                            @Override
                            public void onLongClick(int position) {

                            }
                        });

                    } else {


                        dissmiss();

                        //如果是账号被停用，就踢出到登录页面
                        if (baseModel.getErrorCode().equals(BaseInfo.LogOutCode)) {

                            showConflictDialog(RightsActivity.this, "账号被停用");

                        } else if (baseModel.getErrorCode().equals(BaseInfo.LogOutCode1)) {

                            showConflictDialog(RightsActivity.this, "您的账号在其它设备登录");

                        } else {

                            rlKong.setVisibility(View.VISIBLE);
//                            Toast.makeText(RightsActivity.this, baseModel.getErrorMsg(), Toast.LENGTH_SHORT);
                            return;

                        }


                    }

                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ButterKnife.bind(this);

    }

    @Override
    public void initView() {

        setContentView(R.layout.activity_rights);
    }

    @Override
    public void initData() {

        tvTitleText.setText("权益中心");
        LinearLayoutManager layoutManager = new LinearLayoutManager(RightsActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        xrvRightslist.setLayoutManager(layoutManager);
        xrvRightslist.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        xrvRightslist.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        xrvRightslist.setArrowImageView(R.mipmap.iconfont_downgrey);
        xrvRightslist.setPullRefreshEnabled(false);
        //店铺id
        shopsysno = SharedPreferencesUtil.getString(getApplicationContext(), "shopsysno", "");

        showloading();
        getRightsList();


    }

    @OnClick({R.id.ll_back, R.id.rl_rights_all, R.id.ll_rights_all, R.id.ll_rights_stayok, R.id.rl_rights_bulu, R.id.iv_rightslist, R.id.ll_member_all, R.id.ll_member_stayok, R.id.ll_member_staypay, R.id.ll_member_makeup, R.id.iv_memberlist, R.id.ll_member_bgold,R.id.ll_rights_total
    ,R.id.ll_member_total})
    public void onClick(View view) {
        switch (view.getId()) {


            case R.id.ll_back:

                finish();

                break;

            case R.id.rl_rights_all:
                //点击类型

                if (classflag) {

                    Animation animation = AnimationUtils.loadAnimation(this, R.anim.activity_open_enter);
                    llLei.setAnimation(animation);

                    classflag = false;
                    llLei.setVisibility(View.VISIBLE);
                    xrvRightslist.setClickable(false);
                    ivRightslist1.setImageResource(R.mipmap.jiantou_shang);
                    ivOrderlist2.setImageResource(R.mipmap.jiantou_xia);

                } else {

                    classflag = true;
                    llLei.setVisibility(View.GONE);
                    ivRightslist1.setImageResource(R.mipmap.jiantou_xia);
                    ivOrderlist2.setImageResource(R.mipmap.jiantou_xia);
                    xrvRightslist.setClickable(true);
                    Animation animation = AnimationUtils.loadAnimation(this, R.anim.activity_close_exit);
                    llLei.setAnimation(animation);

                }
                memberflag = true;
                llMember.setVisibility(View.GONE);

                break;
            case R.id.rl_rights_bulu:

                //点击会员等级
                if (memberflag) {
                    Animation animation = AnimationUtils.loadAnimation(this, R.anim.activity_open_enter);
                    llMember.setAnimation(animation);

                    memberflag = false;
                    llMember.setVisibility(View.VISIBLE);
                    xrvRightslist.setClickable(false);
                    ivRightslist1.setImageResource(R.mipmap.jiantou_xia);
                    ivOrderlist2.setImageResource(R.mipmap.jiantou_shang);
                } else {

                    memberflag = true;
                    llMember.setVisibility(View.GONE);
                    xrvRightslist.setClickable(true);
                    ivRightslist1.setImageResource(R.mipmap.jiantou_xia);
                    ivOrderlist2.setImageResource(R.mipmap.jiantou_xia);

                    Animation animation = AnimationUtils.loadAnimation(this, R.anim.activity_close_exit);
                    llMember.setAnimation(animation);

                }

                classflag = true;
                llLei.setVisibility(View.GONE);

                break;
            case R.id.ll_rights_total:
                //全部
                type = "";
                classflag = true;
                llLei.setVisibility(View.GONE);
                ivRightslist1.setImageResource(R.mipmap.jiantou_xia);
                tvRightsAll.setText("全部");

                setselect();

                break;
            case R.id.ll_rights_all:
                //抵扣
                type = "1";
                classflag = true;
                llLei.setVisibility(View.GONE);
                ivRightslist1.setImageResource(R.mipmap.jiantou_xia);
                tvRightsAll.setText("抵扣");
                setselect();

                break;
            case R.id.ll_rights_stayok:
                //抵用
                type = "2";
                classflag = true;
                llLei.setVisibility(View.GONE);
                ivRightslist1.setImageResource(R.mipmap.jiantou_xia);
                tvRightsAll.setText("抵用");

                setselect();

                break;

            case R.id.iv_rightslist:


                classflag = true;
                llLei.setVisibility(View.GONE);
                ivRightslist1.setImageResource(R.mipmap.jiantou_xia);
                Animation animation = AnimationUtils.loadAnimation(this, R.anim.activity_close_exit);
                llLei.setAnimation(animation);


                break;
            case R.id.ll_member_total:

                //全部会员
                customerLevel = "";
                memberflag = true;
                llMember.setVisibility(View.GONE);
                ivOrderlist2.setImageResource(R.mipmap.jiantou_xia);
                tvRightsBulu.setText("全部会员");

                setselect();

                break;
            case R.id.ll_member_all:
                //注册会员
                customerLevel = "100";
                memberflag = true;
                llMember.setVisibility(View.GONE);
                ivOrderlist2.setImageResource(R.mipmap.jiantou_xia);
                tvRightsBulu.setText("注册会员");
                setselect();

                break;
            case R.id.ll_member_stayok:

                //白金尊荣会员
                customerLevel = "200";
                memberflag = true;
                llMember.setVisibility(View.GONE);
                ivOrderlist2.setImageResource(R.mipmap.jiantou_xia);
                tvRightsBulu.setText("白金尊荣会员");

                setselect();

                break;
            case R.id.ll_member_staypay:

                //尊荣会员
                customerLevel = "300";
                memberflag = true;
                llMember.setVisibility(View.GONE);
                ivOrderlist2.setImageResource(R.mipmap.jiantou_xia);

                tvRightsBulu.setText("尊荣会员");

                setselect();

                break;
            case R.id.ll_member_makeup:

                //钻石会员
                customerLevel = "400";
                memberflag = true;
                llMember.setVisibility(View.GONE);
                ivOrderlist2.setImageResource(R.mipmap.jiantou_xia);
                tvRightsBulu.setText("钻石会员");

                setselect();

                break;
            case R.id.iv_memberlist:

                memberflag = true;
                llMember.setVisibility(View.GONE);
                ivOrderlist2.setImageResource(R.mipmap.jiantou_xia);
                Animation animation1 = AnimationUtils.loadAnimation(this, R.anim.activity_close_exit);
                llMember.setAnimation(animation1);

                break;
            case R.id.ll_member_bgold:
                //黑金会员
                customerLevel = "500";
                memberflag = true;
                llMember.setVisibility(View.GONE);
                ivOrderlist2.setImageResource(R.mipmap.jiantou_xia);
                tvRightsBulu.setText("黑金会员");

                setselect();

                break;
        }
    }
    private void setselect() {

        showloading();

        //隐藏空数据
        rlKong.setVisibility(View.GONE);

        if (null != erList && erList.size() > 0) {
            erList.clear();
            //获取数据
            getRightsList();
            rightsAdapter.notifyDataSetChanged();

        } else {

            //获取数据
            getRightsList();
        }



    }
    //获取权益
    private void getRightsList() {

        try {

            //用户id
            HashMap<String, String> sortedParams = new HashMap<String, String>();
            final String url = BaseInfo.URL + "equityController/searchList.do";
            // sysNo 值  3des加密
            //店铺ID
            sortedParams.put("shopStoreSysno", shopsysno);
//            sortedParams.put("shopStoreSysno", "86");
            Log.e(TAG, "店铺ID" + shopsysno);
            //会员等级
//            sortedParams.put("customerLevel", "200");
            sortedParams.put("customerLevel", customerLevel);
            //类型
//            sortedParams.put("type", "2");
            sortedParams.put("type", type);

            OkHttpUtils
                    .post()
                    .url(url)
                    .params((HashMap) MyNetUtils.getInstance().getNetData(sortedParams, this))
                    .build()
                    .execute(new MyStringCallback());

        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    class MyStringCallback extends StringCallback {
        @Override
        public void onError(Call call, Exception e, int id) {
            dissmiss();
            rlKong.setVisibility(View.VISIBLE);
            Log.e(TAG,"=========="+ e.toString());

        }

        @Override
        public void onResponse(String response, int id) {

            Message message = new Message();
            message.what = 1;
            message.obj = response;
            mhandler.sendMessage(message);
        }
    }

    private void dissmiss() {

        if (rotateloading.isStart()) {

            rotateloading.stop();
        }

    }

    private void showloading() {

        rotateloading = (RotateLoading) findViewById(R.id.rotateloading);

        rotateloading.start();
    }


}
